package com.ftl.autopunch.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.ftl.autopunch.entity.SysLog;
import com.ftl.autopunch.entity.SysUser;
import com.ftl.autopunch.service.SysLogService;
import com.ftl.autopunch.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/log")
public class SysLogController {
    @Autowired
    SysLogService sysLogService;
    @Autowired
    SysUserService sysUserService;

    public Long getZeroTimeStamp() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTimeInMillis();
    }

    @GetMapping("/todayPunchStatus")
    public R todayPunchStatus() {
        Long zero = getZeroTimeStamp();
        QueryWrapper<SysLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("last_check", zero, new Date().getTime());
        List<SysLog> sysLogs = sysLogService.list(queryWrapper);
        if (sysLogs.size() > 0) {
            return R.ok("1");
        } else return R.ok("0");
    }

    @GetMapping("/todaySuccessPercent")
    public R todaySuccessPercent() {
        Long zero = getZeroTimeStamp();
        Long now = System.currentTimeMillis();
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("last_check", zero, now);
        List<SysUser> sysUsers = sysUserService.list(queryWrapper);
        int succeed = 0;
        for (SysUser sysUser : sysUsers) {
            if (sysUser.getStatus() == 1) {
                succeed++;
            }
        }
        double percent = (succeed / (double) sysUsers.size()) * 100;
        System.out.println(String.format("%.2f", percent) + "%");
        return R.ok(String.format("%.2f", percent) + "%");
    }

    @GetMapping("/todayLog")
    public R todayLog() {
        Long zero = getZeroTimeStamp();
        QueryWrapper<SysLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("last_check", zero, new Date().getTime());
        List<SysLog> sysLogs = sysLogService.list(queryWrapper);
        return R.ok(sysLogs);
    }

    @GetMapping("/allLog")
    public R allLog() {
        List<SysLog> sysLogs = sysLogService.list();
        return R.ok(sysLogs);
    }

    @GetMapping("/sevenDaysLog")
    public R sevenDaysLog() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 7);
        Long sevenDay = calendar.getTimeInMillis();
        QueryWrapper<SysLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("last_check", sevenDay, new Date().getTime());
        List<SysLog> sysLogs = sysLogService.list();
        return R.ok(sysLogs);
    }
}
